package cn.tedu.csmall.stock.webapi.quartz.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 当前类作用是配置交换机/路由Key/队列的对象和关系
// 属于对当前SpringBoot项目的配置,要添加@Configuration注解
@Configuration
public class RabbitMQConfig {
    // 简单来说就是要将上面各个使用到的对象保存到Spring容器中
    // 交换机/路由Key/队列的名称都定义为常量
    public static final String STOCK_EX="stock_ex";
    public static final String STOCK_ROUT="stock_rout";
    public static final String STOCK_QUEUE="stock_queue";

    // 将交换机对象实例化保存到Spring容器
    @Bean
    public DirectExchange stockDirectExchange(){
        return new DirectExchange(STOCK_EX);
    }
    // 将队列对象实例化保存到Spring容器
    @Bean
    public Queue stockQueue(){
        return new Queue(STOCK_QUEUE);
    }

    // 绑定交换机和队列关系的对象,也就是路由Key
    @Bean
    public Binding stockBinding(){
        return BindingBuilder.bind(stockQueue())
                .to(stockDirectExchange()).with(STOCK_ROUT);
    }

}
